﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using AvalonDock;
using ICSharpCode.AvalonEdit.Folding;
using MaxZhang.EasyEntities.EasyDesigner.Core;
using MaxZhang.EasyEntities.EasyDesigner.UserControls;

namespace MaxZhang.EasyEntities.EasyDesigner
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private DomainModel dm = null;

        FoldingManager foldingManager;
        AbstractFoldingStrategy foldingStrategy;
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_New_Click(object sender, RoutedEventArgs e)
        {
            if (dm == null)
            {
                dm = new DomainModel();
            }
            this.WorkspaceNode.Visibility = Visibility.Visible;
            

        }

        //过滤未配置的表
        private void MenuItem_FilterNoCfg_Click(object sender, RoutedEventArgs e)
        {
            dm.IsFilterNoCfg = !dm.IsFilterNoCfg;
            try
            {
                dm.DataView.Filter = (o) =>
                {
                    var entity = o as DbEntity;
                    if (dm.IsFilterNoCfg)
                    {
                        return !entity.IsConfig;
                    }
                    return true;
                };
            }
            catch { }
            dm.DataView.Refresh();
        }

        //过滤主键
        private void MenuItem_Filter_Click(object sender, RoutedEventArgs e)
        {
            dm.IsFilterKey = !dm.IsFilterKey;
            try
            {
                dm.DataView.Filter = (o) =>
                {
                    var entity = o as DbEntity;
                    if (dm.IsFilterKey)
                    {
                        return string.IsNullOrEmpty(entity.Keys);
                    }
                    return true;
                };
            }
            catch { }
            dm.DataView.Refresh();

        }

     

        private void MenuItem_Open_Click(object sender, RoutedEventArgs e)
        {
            dm.GetTables();
           
            
            DbEntityNode.ItemsSource = dm.DataView;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var entity = (tvWS.SelectedItem as DbEntity);
            DocumentContent document = new DocumentContent();
            document.Title = entity.Name + "设计器";
            document.IsCloseable = true;
            document.FontSize = 12.0;
            TableSchemaList tsl = new TableSchemaList(entity);
            tsl.MW = this;
            document.Content = tsl;
            
            ContentControl.Items.Add(document);
            document.Activate();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer foldingUpdateTimer = new DispatcherTimer();
            foldingUpdateTimer.Interval = TimeSpan.FromSeconds(2);
            foldingUpdateTimer.Tick += foldingUpdateTimer_Tick;
            foldingUpdateTimer.Start();

            this.editer.TextArea.IndentationStrategy = new ICSharpCode.AvalonEdit.Indentation.CSharp.CSharpIndentationStrategy(this.editer.Options);
            foldingStrategy = new BraceFoldingStrategy();

            if (foldingStrategy != null)
            {
                if (foldingManager == null)
                    foldingManager = FoldingManager.Install(this.editer.TextArea);
                foldingStrategy.UpdateFoldings(foldingManager, this.editer.Document);
            }
            else
            {
                if (foldingManager != null)
                {
                    FoldingManager.Uninstall(foldingManager);
                    foldingManager = null;
                }
            }
        }

        void foldingUpdateTimer_Tick(object sender, EventArgs e)
        {
            if (foldingStrategy != null)
            {
                foldingStrategy.UpdateFoldings(foldingManager, this.editer.Document);
            }
        }

        private void MenuItem_OpenFile_Click(object sender, RoutedEventArgs e)
        {
            if(dm!=null)
                dm.DbEntitys.Clear();
            dm = new DomainModel();
            this.WorkspaceNode.Visibility = Visibility.Visible;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开实体";
            ofd.Filter = "(*.dm)|*.dm";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                dm.FileName = ofd.FileName;
               
                DbEntityNode.ItemsSource = dm.DbEntitys;
                dm.Open();

                
                System.Windows.Forms.MessageBox.Show("ok");
            }

        }


        private void MenuItem_Save_Click(object sender, RoutedEventArgs e)
        {
            if (dm.IsOpen)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "保存实体";
                sfd.Filter = "(*.dm)|*.dm";
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if(string.IsNullOrEmpty(dm.FileName))
                        dm.FileName = sfd.FileName;
                    dm.Save();


                    System.Windows.Forms.MessageBox.Show("ok");
                }
                
            }
        }

      

       

    }
}
